package com.RedRiver.hospitalsystem.record_service.app.controller;


import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.record_service.app.utils.TokenGet;
import com.RedRiver.hospitalsystem.record_service.client.PrescriptionClient;
import com.RedRiver.hospitalsystem.record_service.client.param.CreatePrescriptionParam;
import com.RedRiver.hospitalsystem.record_service.client.result.GetPersciptionResult;
import com.RedRiver.hospitalsystem.record_service.form.CreatePrescriptionForm;
import com.RedRiver.hospitalsystem.record_service.form.MedicineForm;
import com.RedRiver.hospitalsystem.record_service.info.GetPersciptionInfo;
import com.RedRiver.hospitalsystem.record_service.service.PrescriptionService;
import com.RedRiver.hospitalsystem.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@RestController
@RequestMapping("/prescription")
@Api(tags = "处方相关")
public class PrescriptionController implements PrescriptionClient {

    @Resource
    private PrescriptionService prescriptionService;

    @Resource
    private TokenGet tokenGet;

    @Override
    @ApiOperation("创建处方")
    public Result<?> createPersciption(CreatePrescriptionParam prescriptionParam) {
        List<MedicineForm> list = BeanUtil.copyToList(prescriptionParam.getList(), MedicineForm.class);
        CreatePrescriptionForm prescriptionForm = BeanUtil.copyProperties(prescriptionParam, CreatePrescriptionForm.class);
        prescriptionForm.setDoctorId(tokenGet.getAccountId());
        prescriptionForm.setList(list);
        return prescriptionService.addPerscription(prescriptionForm);
    }

    @Override
    @ApiOperation("获取处方")
    public Result<GetPersciptionResult> getPersciptionByid(Integer id) {
        GetPersciptionInfo persciptionInfo=prescriptionService.searchPersciption(id);
        GetPersciptionResult result = BeanUtil.copyProperties(persciptionInfo, GetPersciptionResult.class);
        return Result.getInstance(GetPersciptionResult.class).setData(result);
    }
}

